﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using SectionSignAndFilled.MaterialManager;
using SectionSignAndFilled.StairsRoomSection.Kit.Xdata;

namespace SectionSignAndFilled.StairsRoomSection.Kit
{
	internal class makeFillRegion
	{
		public List<FilledRegion> MakeFilledRegion(List<List<CurveLoop>> outSideDiv, ElementId materialId, ElementId viewId, Document doc)
		{
			List<FilledRegion> list = new List<FilledRegion>();
			Transaction transaction = new Transaction(doc, "T1");
			try
			{
				transaction.Start();
				for (int i = 0; i < outSideDiv.Count; i++)
				{
					List<CurveLoop> list2 = outSideDiv[i];
					FilledRegion filledRegion = FilledRegion.Create(doc, materialId, viewId, list2);
					SignXdataForReference.WriteSignComment(filledRegion, viewId);
					list.Add(filledRegion);
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				transaction.RollBack();
			}
			return list;
		}

		public List<FilledRegion> MakeFilledRegionByLinesSort(LinesSort linesSort, MateriaDataManger materiaData, ElementId viewId, Document doc)
		{
			View view = doc.GetElement(viewId) as View;
			List<FilledRegion> list = new List<FilledRegion>();
			List<ElementId> list2 = new List<ElementId>();
			for (int i = 0; i < linesSort.m_lstAllLinesBySort.Count; i++)
			{
				LinesMaterial linesMaterial = linesSort.m_lstAllLinesBySort[i];
				string strFillMaterial = linesMaterial.m_strFillMaterial;
				int scale = view.Scale;
				string text = materiaData.GetFillPatternName(linesMaterial.m_strFillMaterial, scale);
				Color color = materiaData.GetColor(linesMaterial.m_strFillMaterial, (double)scale);
				if (text == "")
				{
					text = "混凝土-钢砼";
				}
				ElementId filledRegionTypeId = this.GetFilledRegionTypeId(text, color, doc);
				List<FilledRegion> list3 = this.MakeFilledRegion(linesMaterial.m_lstcurve3sRes, filledRegionTypeId, viewId, doc);
				list.AddRange(list3);
				if (strFillMaterial.Contains("钢筋混凝土"))
				{
					foreach (FilledRegion filledRegion in list3)
					{
						list2.Add(filledRegion.Id);
					}
				}
			}
			if (list2.Count > 0)
			{
				Transaction transaction = new Transaction(doc, "放置到最前");
				try
				{
					transaction.Start();
					DetailElementOrderUtils.BringToFront(doc, view, list2);
					transaction.Commit();
				}
				catch (Exception)
				{
					transaction.RollBack();
				}
			}
			return list;
		}

		public ElementId GetFilledRegionTypeId(string materialName, Color fillColor, Document document)
		{
			Element element = null;
			string typeName = string.Concat(new object[]
			{
				"楼梯剖面填充类型",
				materialName,
				fillColor.Red,
				"#",
				fillColor.Green,
				"#",
				fillColor.Blue
			});
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
			try
			{
				element = (from e in filteredElementCollector.OfClass(typeof(FilledRegionType))
				where e.Name == typeName
				select e).First<Element>();
			}
			catch
			{
				element = null;
			}
			if (element != null)
			{
				return element.Id;
			}
			element = filteredElementCollector.FirstElement();
			if (element == null)
			{
				return null;
			}
			FillPatternElement fillPatternElement = null;
			FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(document);
			try
			{
				fillPatternElement = ((from e in filteredElementCollector2.OfClass(typeof(FillPatternElement))
				where e.Name == materialName
				select e).First<Element>() as FillPatternElement);
			}
			catch
			{
				return null;
			}
			FilledRegionType filledRegionType = element as FilledRegionType;
			Transaction transaction = new Transaction(document, "tranMoidifStyle");
			ElementType elementType = null;
			try
			{
				transaction.Start();
				elementType = filledRegionType.Duplicate(typeName);
				document.Regenerate();
				Parameter parameter = elementType.get_Parameter(BuiltInParameter.ANY_PATTERN_ID_PARAM_NO_NO);
				parameter.Set(fillPatternElement.Id);
				Color color = (elementType as FilledRegionType).Color;
				if (color.Red != fillColor.Red || color.Green != fillColor.Green || color.Blue != fillColor.Blue)
				{
					(elementType as FilledRegionType).Color = fillColor;
				}
				transaction.Commit();
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				transaction.RollBack();
				return null;
			}
			return elementType.Id;
		}
	}
}
